package com.jucai.pppeiyouclient.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.utils.LoginUtils;
import com.jucai.pppeiyouclient.view.banner.BannerAdapter;
import com.jucai.pppeiyouclient.view.banner.SliderBanner;

import java.util.ArrayList;
import java.util.List;


/**
 * app 引导页
 */
public class GuideActivity extends Activity implements View.OnClickListener {


    SliderBanner sb;
    TextView skip;

    private GuideAdapter adapter;
    private List<Integer> list;
    private boolean isClick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_activity_guide);
        sb = (SliderBanner) findViewById(R.id.sb);
        skip = (TextView) findViewById(R.id.skip);
        list = new ArrayList<>();
        list.add(R.mipmap.guide1);
        list.add(R.mipmap.guide2);
        list.add(R.mipmap.guide3);
        list.add(R.mipmap.guide4);
        adapter = new GuideAdapter(this);
        adapter.setDataList(list);
        sb.setAdapter(adapter);
        sb.setDotNum(list.size());
        skip.setOnClickListener(this);

        sb.setTimeInterval(2000);
        sb.beginPlay();
        sb.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            boolean isLast = false;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (isLast)
                    return;

                if (position == list.size() - 1) {
                    isLast = true;
                    skip.setText("3 跳过");
                    sb.setManualScroll(false);
                    new CountDownTimer(3000, 1000) {

                        @Override
                        public void onTick(long millisUntilFinished) {
                            skip.setText(millisUntilFinished / 1000 + " 跳过");
                        }

                        @Override
                        public void onFinish() {
                            skip.setText("0 跳过");
                            if (!isClick) {
                                Intent intent = new Intent(GuideActivity.this, MainActivity.class);
                                startActivity(intent);
                                GuideActivity.this.finish();
                            }
                        }
                    }.start();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        adapter = null;
        list = null;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.skip) {
            isClick = true;
            gotoMainActivity();
        }
    }

    /**
     * 引导 adapter
     * Created by chenjidong
     */
    public class GuideAdapter extends BannerAdapter {

        private List<Integer> mList = null;

        private Activity activity;

        public GuideAdapter(Activity activity) {
            this.activity = activity;
        }

        @Override
        public View getView(LayoutInflater layoutInflater, int position) {
            View view = layoutInflater.inflate(R.layout.guide_item_list, null);
            ImageView logo = (ImageView) view.findViewById(R.id.iv_logo);
            if (position == mList.size() - 1) {
                logo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        isClick = true;
                        gotoMainActivity();
                    }
                });
            } else
                logo.setOnClickListener(null);
//            Glide.with(layoutInflater.getContext()).load(mList.get(position)).into(logo);
            logo.setImageResource(mList.get(position));
            return view;
        }


        @Override
        public int getCount() {
            return mList.size();
        }

        public void setDataList(List<Integer> list) {
            mList = list;
            notifyDataSetChanged();
        }
    }

    private void gotoMainActivity() {
        if (TextUtils.isEmpty(LoginUtils.getMember_id(this)) || TextUtils.isEmpty(LoginUtils.getToken(this))) {
            startActivity(new Intent(this, LoginActivity.class));
        } else {
            Intent intent = new Intent(GuideActivity.this, MainActivity.class);
            startActivity(intent);
        }
        GuideActivity.this.finish();
//        FirstLaunchUtils.firstLaunch(GuideActivity.this);//加上即可判断 是否首次
        GuideActivity.this.finish();
    }
}
